/**
 * header轮播
 */

var index = 0; // 当前显示的图片 
var swiperItem = document.querySelectorAll('header .swiper-item');
var itemLength = swiperItem.length;
var prevBtn = document.querySelector('header .swiper-button-prev');
var nextBtn = document.querySelector('header .swiper-button-next');

// 向左
prevBtn.onclick = function(){
    activeImg('prev');
};

// 向右
nextBtn.onclick = function(){
    activeImg('next');
}

// 自动
setInterval(function(){
    activeImg('next');
},2000);


/**切换图片 */
function activeImg(position) {
    if (position === 'prev') {
        // 上一张
        if (index <= 0) {
            index = itemLength - 1;
        } else {
            index--;
        }
    } else if (position === 'next') {
        // 下一张
        if (index >= itemLength - 1) {
            index = 0;
        } else {
            index++;
        }
    }
    showImg(index);
};

/**显示指定索引图片 */
function showImg(index) {
    for (var i = 0; i < itemLength; i++) {
        swiperItem[i].classList.remove('show');
    };
    swiperItem[index].classList.add('show');
}